/*
 * Copyright 2011 Philippe Blanc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package me.l1k3.parameter.client;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;

public class Parameter {
    protected Parameter() {
        
    }

    //- - - factory
    
    public final static void load(String name, Callback callback) {
        new Parameter().run(name, null, callback);
    }
    
    public final static void load(String name, String url, Callback callback) {
        new Parameter().run(name, url, callback);
    }
    
    //- - -
    
    private void run(String name, String url, final Callback callback) {
        JavaScriptObject parameters = getFromVar(name);

        if(parameters!=null) {
            callback.run(parameters);
            return;
        }
        
        parameters = getFromMeta(name);
        
        if(parameters!=null) {
            callback.run(parameters);
            return;
        }
        
        if(url!=null) {
            RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
            
            try {
                builder.sendRequest(name, new RequestCallback() {
                    @Override
                    public void onResponseReceived(Request request, Response response) {
                        if(response.getStatusCode()<400) {
                            callback.run(getFromJSON(response.getText()));
                        }
                        else {
                            callback.run(empty());
                        }
                    }
                    
                    @Override
                    public void onError(Request request, Throwable exception) {
                        callback.run(empty());
                    }
                });
            }
            catch(RequestException e) {
                callback.run(empty());
            }
        }
        else {
            callback.run(empty());
        }
    }
    
    private native JavaScriptObject getFromVar(String name) /*-{
        if(typeof $wnd[name]=='undefined' || typeof $wnd[name].tagName!='undefined') return null;
        if(typeof $wnd[name]=='string') return eval('('+$wnd[name]+')');
        if(typeof $wnd[name]=='object') return $wnd[name];
        
        return null;
    }-*/;
    
    private native JavaScriptObject getFromMeta(String name) /*-{
        var metas = $doc.getElementsByTagName("meta");
        for ( var i = 0, n = metas.length; i < n; ++i) {
            if (metas[i].getAttribute("name")==name) return eval('('+metas[i].getAttribute("content")+')');
        }
        return null;
    }-*/;
    
    private native JavaScriptObject getFromJSON(String input) /*-{
        return eval('('+input+')');
    }-*/;
    
    private native JavaScriptObject empty() /*-{
        return { };
    }-*/;
}
